package EstimatorPackage;

import EstimatorPackage.Expressions.Expression;
import EstimatorPackage.Expressions.SimpleExpression;
import DataSetPackage.ProductView;
import DataSetPackage.View;
import HierarchyPackage.HClass;
import HierarchyPackage.Hierarchy;
import HierarchyPackage.ProductHierarchy.ProductHClass;
import HierarchyPackage.ProductHierarchy.ProductHierarchy;


/**
 *
 * @author Martin Haulrich
 */
public class IndependentProductEst extends AbstractEstimatorExp {

    EstimatorExp[] partEstimator;

    int cH = 0;

    public IndependentProductEst(EstimatorExp[] partEstimator) {
        super();
        this.partEstimator = partEstimator;
    }

    @Override
    public double getEstimate(Integer did, View v) {
        ProductView pv = (ProductView) v;
//        int[] dids = pv.getDataIDs(did);
        View[] views = pv.getViews();
        double est = 0;
        for (int i = 0; i < views.length; i++) {
            double thisest = partEstimator[i].getEstimate(did, views[i]);
            est = est + thisest;
        }
//
//        double condEst = 0;
//        for (int i = 0; i < dids.length - 1; i++) {
//            double thisest = partEstimator[i].getEstimate(dids[i], views[i]);
//            condEst = condEst + thisest;
//        //SimpleView sv = (SimpleView) views[i];
//        //      System.err.println("DataEst-"+i+": " +dids[i] + "    "+ sv.getFeatures(dids[i])[0] + " : " + thisest);
//        }
//
//        return est - condEst;
//        int index = dids.length-1;
//        return partEstimator[index].getEstimate(dids[index], views[index]);
        return est;
    }

    public double getEstimate(HClass h, Hierarchy hier) {

        ProductHClass ph = (ProductHClass) h;
        ProductHierarchy phier = (ProductHierarchy) hier;

        double est = 0;

        HClass[] classes = ph.getID();

        for (int i = 0; i < classes.length; i++) {

            Expression ce = new SimpleExpression(classes[i]);
            double cest = partEstimator[i].getEstimate(ce, phier.getHierarchies()[i]);
//                  System.err.println(classes[i].getName() + " : " + cest);
            est = est + cest;
        }

        return est;
    }
}
